<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input size="mini" v-model="listQuery.tradNo" clearable class="filter-item" style="width: 200px;"
        placeholder="订单编号" />
      <el-input size="mini" v-model="listQuery.companyName" clearable class="filter-item" style="width: 200px;"
        placeholder="企业名称" />
      <el-input size="mini" v-model="listQuery.categoryName" clearable class="filter-item" style="width: 200px;"
        placeholder="分类名称" />
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找
      </el-button>
      <!--<el-button  class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>-->
      <!-- <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button> -->
    </div>
    <!-- 类型-->
    <div class="filter-container">
      <el-tag v-for="(tag,index) in tags" :key="tag.name" :class="bgindex === index?'active':''"
        style="cursor: pointer;margin-right: 10px" @click.native="tagclick(tag.cid,index)">
        {{ tag.name }}
      </el-tag>
    </div>
    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" border size="mini" element-loading-text="正在查询中。。。"
      highlight-current-row>
      <el-table-column align="center" label="订单编号" width="250px" prop="tradNo" />
      <!--      <el-table-column align="center" label="订单编号"  prop="tradNo">
        <template slot-scope="scope">
          <el-popover placement="right"  trigger="hover">
            <span slot="reference">编号</span>
            <span>{{ scope.row.tradNo }}</span>
          </el-popover>
        </template>
      </el-table-column>-->
      <el-table-column align="center" label="企业名称" prop="companyName" width="250px" />
      <!--<el-table-column align="center" label="行业名称" prop="categoryName"/>-->
      <el-table-column align="center" label="缴费人数" prop="payPeopleNum" width="80px" />
      <!--<el-table-column align="center" label="课时单价" prop="price"/>-->
      <el-table-column align="center" label="总金额" prop="payAmount" width="80px" />

      <el-table-column align="center" label="支付情况" prop="payStatus" width="100px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.payStatus===0" type="info">未支付</el-tag>
          <el-tag v-if="scope.row.payStatus===1" type="primary">已付款</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="缴费情况" prop="confirmPay" width="100px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.confirmPay===0" type="info">未收款</el-tag>
          <el-tag v-if="scope.row.confirmPay===1" type="success">已收款</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="支付类型" prop="payType" width="80px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.payType===1" type="primary">微信</el-tag>
          <el-tag v-if="scope.row.payType===2" type="success">线下</el-tag>
        </template>
      </el-table-column>

      <!--<el-table-column align="center" label="下单时间" prop="createDate"/>-->
      <el-table-column align="center" label="下单时间" prop="createDate" width="80px">
        <template slot-scope="scope">
          <el-popover placement="right" trigger="hover">
            <span slot="reference">详情</span>
            <span>{{ scope.row.createDate }}</span>
          </el-popover>
        </template>
      </el-table-column>


      <el-table-column align="center" label="发票" prop="invoiceUrl" width="120px">
        <template slot-scope="scope">
          <el-popover placement="right" title="" trigger="hover">
            <img :src="scope.row.invoiceUrl" style="width:300px;height:300px;">
            <img slot="reference" :src="scope.row.invoiceUrl" style="width:100px;heigth:100px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" label="转账凭证" prop="payEvidence" width="120px">
        <template slot-scope="scope">
          <el-popover placement="right" title="" trigger="hover">
            <img :src="scope.row.payEvidence" style="width:300px;height:300px;">
            <img slot="reference" :src="scope.row.payEvidence" style="width:100px;heigth:100px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button class="filter-item" style="width:35%;" type="primary" size="mini" @click="handleUpdate(scope.row)">
            上传发票</el-button>
          <el-button class="filter-item" style="width:35%;" v-if="scope.row.confirmPay===0" type="success" size="mini"
            @click="handleStatus(scope.row,1)">确认收款</el-button>
          <!--<el-button  type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px"
        style="width: 400px; margin-left:50px;">

        <el-form-item label="上传发票" prop="invoiceUrl">
          <el-upload :headers="headers" :action="uploadPath" :show-file-list="false" :on-success="uploadPicUrl"
            class="avatar-uploader" accept=".jpg,.jpeg,.png,.gif">
            <img v-if="dataForm.invoiceUrl" :src="dataForm.invoiceUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <!--<el-form-item label="上传凭证" prop="payEvidence">-->
        <!--<el-upload-->
        <!--:headers="headers"-->
        <!--:action="uploadPath"-->
        <!--:show-file-list="false"-->
        <!--:on-success="uploadPicUrl1"-->
        <!--class="avatar-uploader"-->
        <!--accept=".jpg,.jpeg,.png,.gif">-->
        <!--<img v-if="dataForm.payEvidence" :src="dataForm.payEvidence" class="avatar">-->
        <!--<i v-else class="el-icon-plus avatar-uploader-icon"/>-->
        <!--</el-upload>-->
        <!--</el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">取消</el-button>
        <el-button size="mini" v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button size="mini" v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<style>
  .active {
    background-color: #1e6abc;
    color: white;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #20a0ff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }

  .avatar {
    width: 145px;
    height: 145px;
    display: block;
  }
</style>

<script>
  import {
    listOrder,
    createOrder,
    updateOrder,
    deleteOrder
  } from '@/api/order'
  import {
    uploadPath
  } from '@/api/storage'
  import {
    getToken
  } from '@/utils/auth'
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

  export default {
    name: 'Order',
    components: {
      Pagination
    },
    data() {
      return {
        uploadPath,
        updateParam: undefined,
        list: undefined,
        total: 0,
        listLoading: true,
        listQuery: {
          id: undefined,
          page: 1,
          limit: 20,
          companyId: undefined,
          tradNo: undefined,
          payType: undefined,
          companyName: undefined,
          categoryName: undefined,
          sort: 'id',
          order: 'desc'
        },
        dataForm: {
          id: undefined,
          tradNo: undefined,
          companyId: undefined,
          companyName: undefined,
          categoryId: undefined,
          categoryName: undefined,
          payPeopleNum: undefined,
          payAmount: undefined,
          payType: undefined,
          payStatus: undefined,
          payEvidence: undefined,
          invoiceUrl: undefined,
          confirmPay: undefined,
          createDate: undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '创建'
        },
        rules: {
          name: [{
            required: true,
            message: '商家名称不能为空',
            trigger: 'blur'
          }]
        },
        downloadLoading: false,
        tags: [{
            name: '全部',
            cid: ''
          },
          {
            name: '微信',
            cid: 1
          },
          {
            name: '线下',
            cid: 2
          }
        ],
        bgindex: -1
      }
    },
    computed: {
      headers() {
        return {
          'X-Litemall-Admin-Token': getToken()
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      // 标签类事件
      tagclick(cid, index) {
        if (this.bgindex === index) {
          this.bgindex = -1
          this.listQuery.payType = ''
          this.getList()
        } else {
          this.bgindex = index
          this.listQuery.payType = cid
          this.getList()
        }
      },
      getList() {
        this.listLoading = true
        listOrder(this.listQuery)
          .then(response => {
            this.list = response.data.data.items
            this.total = response.data.data.total
            this.listLoading = false
          })
          .catch(() => {
            this.list = []
            this.total = 0
            this.listLoading = false
          })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      resetForm() {
        this.dataForm = {
          id: undefined,
          tradNo: undefined,
          companyId: undefined,
          companyName: undefined,
          categoryId: undefined,
          categoryName: undefined,
          payPeopleNum: undefined,
          payAmount: undefined,
          payType: undefined,
          payStatus: undefined,
          payEvidence: undefined,
          invoiceUrl: undefined,
          confirmPay: undefined,
          createDate: undefined
        }
      },
      handleCreate() {
        this.resetForm()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      uploadPicUrl: function(response) {
        this.dataForm.invoiceUrl = response.data.url
      },
      uploadPicUrl1: function(response) {
        this.dataForm.payEvidence = response.data.url
      },
      createData() {
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            console.log(this.dataForm)
            createOrder(this.dataForm)
              .then(response => {
                this.list.unshift(response.data.data)
                this.dialogFormVisible = false
                this.$notify.success({
                  title: '成功',
                  message: '创建成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.data.message
                })
              })
          }
        })
      },
      handleStatus(parm, status) {
        this.updateParam = Object.assign({}, parm)
        this.updateParam.confirmPay = status
        let content = ''
        if (status === 1) {
          content = '确认已收款？？？'
        } else {
          content = '！！'
        }
        this.$confirm(content, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (status === 1) {
            updateOrder(this.updateParam)
              .then(() => {
                this.$notify.success({
                  title: '成功',
                  message: '已确认收款'
                })
                // 刷新列表数据
                this.getList()
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.data.message
                })
                // 刷新列表数据
                this.getList()
              })
          } else {
            updateOrder(this.updateParam)
              .then(() => {
                this.$notify.success({
                  title: '成功',
                  message: '已对其他用户隐藏该评论'
                })
                // 刷新列表数据
                this.getList()
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.data.message
                })
                // 刷新列表数据
                this.getList()
              })
          }
        }).catch(() => {
          this.getList()
        })
      },
      handleUpdate(row) {
        this.dataForm = Object.assign({}, row)
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            updateOrder(this.dataForm)
              .then(() => {
                for (const v of this.list) {
                  if (v.id === this.dataForm.id) {
                    const index = this.list.indexOf(v)
                    this.list.splice(index, 1, this.dataForm)
                    break
                  }
                }
                this.dialogFormVisible = false
                this.$notify.success({
                  title: '成功',
                  message: '更新成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.data.message
                })
              })
          }
        })
      },
      handleDelete(row) {
        deleteOrder(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.message
            })
          })
      },
      handleDownload() {
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = [
            '商家ID',
            '商家名称',
            '介绍',
            '低价',
            '商家图片'
          ]
          const filterVal = ['id', 'name', 'desc', 'floorPrice', 'picUrl']
          excel.export_json_to_excel2(
            tHeader,
            this.list,
            filterVal,
            '商家信息'
          )
          this.downloadLoading = false
        })
      }
    }
  }
</script>
